package top.lgd.service.study;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lgd.base.LgdConstants;
import top.lgd.enums.DateEnum;
import top.lgd.pojo.res.study.CatalogDurationRes;
import top.lgd.pojo.res.study.DayDurationLine;
import top.lgd.pojo.res.study.DayDurationRes;
import top.lgd.pojo.res.study.StudyOverviewRes;
import top.lgd.service.LgdCacheService;
import top.lgd.utils.CollectionUtils;
import top.lgd.utils.DateUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 学习内容表 服务实现类
 *
 * @author YYJ
 * @version 1.0
 * @since 2023/12/15 10:10
 */
@Service
public class OverviewService {

    @Autowired
    private RecordService recordService;
    @Autowired
    private LgdCacheService lgdCacheService;

    /**
     * 【有缓存】
     *
     * @return
     */
    public StudyOverviewRes queryOverview(Long userId) {
        final String key = LgdConstants.KEY_STUDY_OVERVIEW_INFO + DateUtils.today() + ":" + userId;
        StudyOverviewRes studyOverviewRes = lgdCacheService.get(key);
        if (null != studyOverviewRes) {
            return studyOverviewRes;
        }

        studyOverviewRes = new StudyOverviewRes();
        studyOverviewRes.setTodayDuration(recordService.sumDuration(userId, DateEnum.TD));
        studyOverviewRes.setYesterdayDuration(recordService.sumDuration(userId, DateEnum.YD));
        studyOverviewRes.setTodaySub(studyOverviewRes.getTodayDuration().subtract(studyOverviewRes.getYesterdayDuration()));
        studyOverviewRes.setTodayTrend(studyOverviewRes.getTodaySub().compareTo(BigDecimal.ZERO) > 0 ? LgdConstants.UP : LgdConstants.DOWN);

        studyOverviewRes.setWeekDuration(recordService.sumDuration(userId, DateEnum.TW));
        studyOverviewRes.setLastWeekDuration(recordService.sumDuration(userId, DateEnum.LW));
        studyOverviewRes.setWeekSub(studyOverviewRes.getWeekDuration().subtract(studyOverviewRes.getLastWeekDuration()));
        studyOverviewRes.setWeekTrend(studyOverviewRes.getWeekSub().compareTo(BigDecimal.ZERO) > 0 ? LgdConstants.UP : LgdConstants.DOWN);

        studyOverviewRes.setMonthDuration(recordService.sumDuration(userId, DateEnum.TM));
        studyOverviewRes.setLastMonthDuration(recordService.sumDuration(userId, DateEnum.LM));
        studyOverviewRes.setMonthSub(studyOverviewRes.getMonthDuration().subtract(studyOverviewRes.getLastMonthDuration()));
        studyOverviewRes.setMonthTrend(studyOverviewRes.getMonthSub().compareTo(BigDecimal.ZERO) > 0 ? LgdConstants.UP : LgdConstants.DOWN);

        studyOverviewRes.setYearDuration(recordService.sumDuration(userId, DateEnum.TY));
        studyOverviewRes.setLastYearDuration(recordService.sumDuration(userId, DateEnum.LY));
        studyOverviewRes.setYearSub(studyOverviewRes.getYearDuration().subtract(studyOverviewRes.getLastYearDuration()));
        studyOverviewRes.setYearTrend(studyOverviewRes.getYearSub().compareTo(BigDecimal.ZERO) > 0 ? LgdConstants.UP : LgdConstants.DOWN);

        lgdCacheService.put(key, studyOverviewRes);
        return studyOverviewRes;
    }

    /**
     * 【有缓存】
     *
     * @return
     */
    public List<DayDurationLine> queryDayDurationList(Long userId) {
        final String key = LgdConstants.KEY_STUDY_DURATION_LIST + DateUtils.today() + ":" + userId;

        List<DayDurationLine> durationLineList = lgdCacheService.get(key);
        if (CollectionUtils.isNotEmpty(durationLineList)) {
            return durationLineList;
        }

        durationLineList = new ArrayList<>(64);

        List<DayDurationRes> durationResList = recordService.queryDurationList(userId);
        for (DayDurationRes d : durationResList) {
            durationLineList.add(new DayDurationLine(d.getStudyDay(), "时长", d.getDuration()));
            durationLineList.add(new DayDurationLine(d.getStudyDay(), "进度", d.getCurrentIndex()));
        }

        lgdCacheService.put(key, durationLineList);
        return durationLineList;
    }

    /**
     * 【有缓存】
     *
     * @return
     */
    public List<CatalogDurationRes> queryCatalogDurationList(Long userId) {
        final String key = LgdConstants.KEY_STUDY_CATALOG_DURATION_LIST + DateUtils.today() + ":" + userId;

        List<CatalogDurationRes> catalogDurationResList = lgdCacheService.get(key);
        if (CollectionUtils.isNotEmpty(catalogDurationResList)) {
            return catalogDurationResList;
        }

        catalogDurationResList = recordService.catalogDurationList(userId);
        lgdCacheService.put(key, catalogDurationResList);
        return catalogDurationResList;
    }
}
